define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'vue'], function ($, undefined, Backend, Table, Form,Vue) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xykeep/user.course/index' + location.search,
                    add_url: 'xykeep/user.course/add',
                    multi_url: 'xykeep/user.course/multi',
                    import_url: 'xykeep/user.course/import',
                    table: 'xykeep_user_course',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'user.nickname', title: __('会员信息'),operate: 'LIKE',formatter:function(v,row){
                                var html = '<div style="display:flex"><img width="50px" height="50px" src="'+row.user.avatar+'" /><p style="text-align:left;line-height:20px;margin-top:5px;margin-left:10px">ID:'+row.user.id+'<br/>'+v+'</p></div>';
                                return html;
                            }
                        },
                        {field: 'name', title: __('课程'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'store.name', title: __('上课门店')},
                        {field: 'coach.realname', title: __('上课教练')},
                        //{field: 'type', title: __('Type'), operate: 'LIKE'},
                        {field: 'nums', title: __('课程总节数'),operate:false},
                        {field: 'use_nums', title: __('上课节数'),operate:false},
                        {field: 'sy', title: __('剩余节数'),operate:false,
                        formatter:function(v,row){
                                return row.nums - row.use_nums;
                            }
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons :[
                                {
                                    name: 'detail',
                                    text: __('明细'),
                                    title: function(row){
                                        return row.user.nickname + '的 '+row.name+' 课程明细'
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-eye',
                                    extend: 'data-area=\'["80%", "80%"]\'',
                                    url: function (row) {
                                        return `xykeep/user.course_log?user_course_id=${row.id}`;
                                    }
                                },
                                {
                                    name: 'increase',
                                    text: __('加课'),
                                    title:  __('增加课程节数'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-plus-square',
                                    extend: 'data-area=\'["50%", "50%"]\'',
                                    url: function (row) {
                                        return `xykeep/user.course/increase?ids=${row.id}`;
                                    }
                                },
                                {
                                    name: 'reduce',
                                    text: __('减课'),
                                    title:  __('减少课程节数'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-plus-square',
                                    extend: 'data-area=\'["50%", "50%"]\'',
                                    url: function (row) {
                                        return `xykeep/user.course/reduce?ids=${row.id}`;
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xykeep.user.course/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xykeep.user.course/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xykeep.user.course/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        increase: function () {
            Controller.api.bindevent();
        },
        reduce: function () {
            Controller.api.bindevent();
        },
        import: function () {
            Controller.api.bindevent();
        },
        add: function () {
            new Vue({
                el: "#userCourse",
                data() {
                    return {
                        store_id:'',//门店ID
                        store_name:'',//门店名称
                        coach_id:'',//教练ID
                        coach_realname:'',//教练姓名
                        course_id:'',//课程ID
                        course_name:'',//课程名称
                    }
                },
                methods: {
                    selectStore(){
                        var that = this;
                        parent.Fast.api.open("xykeep/store/select?multiple=false", __('选择门店'), {
                            area: ['80%', '80%'],
                            callback: function (data) {
                                that.store_id = data.url;
                                that.store_name = data.name;
                                that.coach_id = '';	
                                that.coach_realname = '';
                                that.course_id = '';
                                that.course_name = '';
                            }
                        });
                    },
                    selectCoach(){
                        var that = this,store_id = this.store_id;
                        if(!store_id){
                            Toastr.error("请先选择上课门店！");
                        }else{
                            parent.Fast.api.open("xykeep/coach/select?store_id="+store_id+"&multiple=false", __('选择教练'), {
                                area: ['80%', '80%'],
                                callback: function (data) {
                                    that.coach_id = data.url;
                                    that.coach_realname = data.realname;	
                                    that.course_id = '';
                                    that.course_name = '';
                                }
                            });
                        }
                    },
                    selectCourse(){
                        var that = this,coach_id = this.coach_id;
                        if(!coach_id){
                            Toastr.error("请先选择上课教练！");
                        }else{
                            parent.Fast.api.open("xykeep/course/select?coach_id="+coach_id+"&multiple=false", __('选择课程'), {
                                area: ['80%', '80%'],
                                callback: function (data) {
                                    that.course_id = data.id;
                                    that.course_name = data.name;	
                                }
                            });
                        }
                    },
                },
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
